package test_front.sevice.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import test_common.entity.GoldAndPoints;
import test_common.entity.GoldAndPointsExample;
import test_common.entity.User;
import test_common.entity.UserExample;
import test_front.exception.LoginNameExistException;
import test_front.mapper.GoldAndPointsMapper;
import test_front.mapper.UserMapper;
import test_front.sevice.UserService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRED)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GoldAndPointsMapper goldAndPointsMapper;

    @Override
    public void register(User user) {
       String date= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        try {
            user.setCreateDate( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setStatus(1);
        userMapper.insertSelective(user);
    }

    @Override
    public User selectLoginName(String loginName) {
        UserExample example=new UserExample();
        example.or()
                .andLoginNameEqualTo(loginName);
        List<User> user=userMapper.selectByExample(example);
        return user.isEmpty()?null:user.get(0);
    }

    @Override
    public User selectByNameAndPassword(User user) {
        UserExample example=new UserExample();
        example.or()
                .andLoginNameEqualTo(user.getLoginName())
                .andPasswordEqualTo(user.getPassword());
        List<User> user01 = userMapper.selectByExample(example);
        return user01.isEmpty()?null:user01.get(0);
    }

    @Override
    public void insertLoginDate(User user) {
        UserExample example=new UserExample();
        example.or()
                .andIdEqualTo(user.getId());
        userMapper.updateByExampleSelective(user,example);
    }

    @Override
    public GoldAndPoints selectSelfGoldAndPoints(String userId) {
        GoldAndPointsExample example=new GoldAndPointsExample();
        return null;
    }
}
